package com.power.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.power.domain.SysLog;
import com.power.mapper.SysLogMapper;
import com.power.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Service
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements SysLogService{
    @Autowired
    private  SysLogMapper sysLogMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    private final static String LOG_KEY="log:";
    @Override
    public Page getlistpage(Page<SysLog> page, Integer userId,String operation,int current) {
        String s1 = stringRedisTemplate.opsForValue().get(LOG_KEY+current);
        if (!StringUtils.hasText(s1)) {
            LambdaQueryWrapper<SysLog> queryWrapper=new LambdaQueryWrapper<>();
            if (StringUtils.hasText(operation))
            {
                queryWrapper.like(SysLog::getOperation,operation);
            }
            if (userId!=null)
            {
                queryWrapper.eq(SysLog::getUserId,userId);
            }
            Page<SysLog> page1 = sysLogMapper.selectPage(page, queryWrapper);
            JSONObject jsonObject=new JSONObject();
            String s = jsonObject.toJSONString(page1);
            stringRedisTemplate.opsForValue().set(LOG_KEY+current,s,60*60*5, TimeUnit.SECONDS);
            return   sysLogMapper.selectPage(page,queryWrapper);
        }
        return JSONObject.parseObject(s1,Page.class);


    }
}
